package com.huluobo.user

import android.util.Log
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.huluobo.user.entity.ApproveEntityItem
import java.lang.Exception

/**
 *  Created by LC on 2025/4/6.
 */
class ApprovePagingSource(private val userRepo: UserRepo) : PagingSource<Int, ApproveEntityItem>() {
    override fun getRefreshKey(state: PagingState<Int, ApproveEntityItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ApproveEntityItem> {
        return try {
            val currentPage = params.key ?: 1
            val size = 10
            val response = userRepo.getApprovePage(currentPage.toString(), size.toString())
            val items = response.data
            var sum = 0
            sum += items.size
            Log.i("TagA", "load:items:${sum}")

            val preKey = if (currentPage > 1) currentPage - 1 else null
            val nextKey = if (items.isNotEmpty()) currentPage + 1 else null
            LoadResult.Page(items, preKey, nextKey)
        } catch (e: Exception) {
            Log.e("TagA", "paging error:${e.message}")
            LoadResult.Error(e)
        }
    }
}